package com.portal.module.notice.web;

import com.portal.module.notice.service.NoticeService;
import com.portal.module.notice.entity.Notice;
import com.portal.admin.base.entity.BaseAction;
import com.portal.admin.base.entity.ResultInfo;
import com.portal.admin.utils.admin.StringUtils;
import com.portal.admin.base.entity.DateUtils;
import com.portal.admin.utils.excel.ExportExcel;
import com.portal.admin.utils.excel.ImportExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
*【通知信息】页面接口
*/
@RestController
@RequestMapping("/admin/notice")
public class NoticeController extends BaseAction {

    @Autowired
    private NoticeService noticeService;

    /**
    * 【通知信息】根据条件分页查询
    * @param page
    * @param notice
    * @return
    */
    @RequestMapping("/getPage")
    @RequiresPermissions("notice:getPage")
    public ResultInfo getPage(Page<Notice> page, Notice notice) {
        IPage<Notice> iPage = noticeService.getPage(page, notice);
        return ResultInfo.ok("获取分页成功", iPage);
    }

    /**
    * 【通知信息】根据条件查询
    * @param notice
    * @return
    */
    @RequestMapping("/getList")
    public ResultInfo getList(Notice notice) {
        List<Notice> list = noticeService.getList(notice);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
    * 【通知信息】根据id查询
    * @param id
    * @return
    */
    @RequestMapping("/get")
    @RequiresPermissions({"notice:edit","notice:view"})
    public ResultInfo get(String id) {
        Notice notice = noticeService.get(id);
        return ResultInfo.ok("获取对象成功", notice);
    }

    /**
    * 【通知信息】提交(新增或修改)
    * @param notice
    * @return
    */
    @RequestMapping("/sub")
    @RequiresPermissions("notice:save")
    public ResultInfo insert(Notice notice) {
        if (StringUtils.isEmpty(notice.getId())) { //新增
            noticeService.insert(notice);
        } else {//修改
            noticeService.update(notice);
        }
        return ResultInfo.ok("提交成功!");
    }

    /**
    * 【通知信息】删除
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    @RequiresPermissions("notice:delete")
    public ResultInfo delete(String id) {
        noticeService.delete(id);
        return ResultInfo.ok("删除成功!");
    }


    /**
    *【通知信息】导出
    */
    @RequestMapping(value = "/export")
    @RequiresPermissions("notice:export")
    public void exportFile(Notice notice, HttpServletResponse response) {
        try {
            String fileName = "通知信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<Notice> list = noticeService.getList(notice);
            new ExportExcel("通知信息", Notice.class).setDataList(list).write(response, fileName).dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
    *【通知信息】导入
    */
    @RequestMapping(value = "/import")
    @RequiresPermissions("notice:import")
    public ResultInfo importFile(MultipartFile file) {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<Notice> list = ei.getDataList(Notice.class);
            for (Notice notice : list) {
                try {
                    noticeService.insert(notice);
                    successNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条通知信息记录。");
            }
            return ResultInfo.ok("已成功导入 " + successNum + " 条通知信息记录" + failureMsg);
        } catch (Exception e) {
            return ResultInfo.error("导入通知信息失败！失败信息：" + e.getMessage());
        }
    }

    /**
    *【通知信息】模板下载
    */
    @RequestMapping(value = "/import/template")
    @RequiresPermissions("notice:import")
    public ResultInfo importFileTemplate(HttpServletResponse response) {
        try {
            String fileName = "通知信息数据导入模板.xlsx";
            List<Notice> list = new ArrayList<>();
            new ExportExcel("通知信息数据", Notice.class, 1).setDataList(list).write(response, fileName).dispose();
            return ResultInfo.ok("下载模板成功！");
        } catch (Exception e) {
            return ResultInfo.error("导入模板下载失败！失败信息：" + e.getMessage());
        }
    }

}



